package com.imooc.demo.controller;

import com.imooc.demo.converter.OrderForm2OrderDTOConverter;
import com.imooc.demo.dto.OrderDTO;
import com.imooc.demo.enums.ResultEnum;
import com.imooc.demo.exception.SellException;
import com.imooc.demo.form.OrderForm;
import com.imooc.demo.service.BuyerService;
import com.imooc.demo.service.OrderService;
import com.imooc.demo.utils.PageResultVoUtil;
import com.imooc.demo.utils.ResultVoUtil;
import com.imooc.demo.vo.PageResultVo;
import com.imooc.demo.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @Auther: 杨通通
 * @Date: 2021/11/7 19:38
 * @Description:
 */

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    //    创建订单
    @PostMapping("/create")
    public ResultVo create(@Valid @RequestBody OrderForm orderForm, BindingResult bindingResult){
        System.out.println(bindingResult.hasErrors());
        if(bindingResult.hasErrors()){
            log.error("[创建订单]参数不正确,param={}",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("[创建订单]购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO result = orderService.create(orderDTO);
        HashMap<String , String> resultMap = new HashMap<>();
        resultMap.put("orderId",result.getOrderId());
        return ResultVoUtil.success(resultMap);

    }


//    订单列表
    @GetMapping("/list")
    public PageResultVo list(@RequestParam("openid") String openid,@RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "10") Integer pageSize){
            if(openid.isEmpty()){
                throw new SellException(ResultEnum.PARAM_ERROR.getCode(),"openid不能为空");
            }
            PageRequest page = PageRequest.of(pageIndex - 1, pageSize);
        Page<OrderDTO> result = orderService.findList(openid, page);
        Integer total = Math.toIntExact(result.getTotalElements());
       return PageResultVoUtil.success(result.getContent(),pageIndex,total);
    }


//    订单详情
    @GetMapping("/detail")
    public ResultVo detail(String openid,String orderId){
        if(openid.isEmpty()){
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),"openid不能为空");
        }
        if(orderId.isEmpty()){
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),"orderId不能为空");
        }
        //todo 不可跨域访问
        OrderDTO orderDetail = buyerService.findOrderOne(openid, orderId);
        return ResultVoUtil.success(orderDetail);

    }
//    取消订单
    @GetMapping("/cancel")
    public ResultVo cancel(String openid,String orderId){
        if(openid.isEmpty()){
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),"openid不能为空");
        }
        if(orderId.isEmpty()){
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),"orderId不能为空");
        }
       buyerService.cancelOrder(openid,orderId);

        return ResultVoUtil.success();
    }

}